<?php
  require_once('./includes/header.php');

  $id = (isset($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : 0;

$user = fetchById('users',$id);
pr($user);

$isMale = $user['gender']=='m' ? 'checked':'';
$isFemale = $user['gender']=='f' ? 'checked':'';

$countries = getCountries();
$languages = getLanguages();
$hobbies = getHobbies();
?>
<!------------HEADER ENDS--------->

      <div class="row">
<!----------------  FORM ------------>
<form class="form-horizontal" method="post"
enctype="multipart/form-data"
action="edit_saveuser.php" id="signupForm" role="form" >

<input type="hidden" name="id" value="<?=$id?>" />
<input type="hidden" name="avatar" value="<?=$user['avatar']?>" />

  <div class="form-group">
    <label  class="col-sm-3 control-label">Name</label>
    <div class="col-sm-5">

      <input type="text" value="<?=$user['uname']?>" autocomplete="off" class="form-control" name="uname" placeholder="Name">

    </div>
  </div>

  <div class="form-group">
    <label  class="col-sm-3 control-label">Email</label>
    <div class="col-sm-5">
      <input type="email" value="<?=$user['email']?>" autocomplete="off" class="form-control"  name="email" disabled placeholder="Email">
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">Date of Birth</label>
    <div class="col-sm-5">

      <div class='input-group date' id='datetimepicker1' data-date-format="YYYY/MM/DD">
        <input readonly type='text' value="<?=$user['dob']?>" name="dob" class="form-control" />
        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
        </span>
      </div>
      <label for="dob" class="error"></label>

   </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">Gender</label>
    <div class="col-sm-5">

        <label class="radio-inline">
          <input type="radio" <?=$isMale?> name="gender" value="m">
          Male
        </label>

        <label class="radio-inline">
          <input type="radio" <?=$isFemale?> name="gender"   value="f">
          Female
        </label>

        <label class="error" for="gender" style="width: 100%;"></label>


    </div>
  </div>


  <div class="form-group">
    <label  class="col-sm-3 control-label">Address</label>
    <div class="col-sm-5">
      <textarea class="form-control" name="address" rows="3"><?=$user['address']?></textarea>
    </div>
  </div>


  <div class="form-group">
    <label  class="col-sm-3 control-label">Country</label>

    <div class="col-sm-5">
      <select class="form-control" name="country_id">
        <option value="">-Select-</option>
<?php
foreach($countries as $cId=>$cName) {
  $sel_country = ($cId == $user['country_id']) ? 'selected':'';

?>
       <option <?=$sel_country?> value="<?=$cId?>"><?=$cName?></option>
<?php
}
?>
      </select>
    </div>
  </div>

  <div class="form-group">
    <label  class="col-sm-3 control-label">Languages Known</label>
    <div class="col-sm-5">
      <select multiple name="lang[]" class="form-control">
<?php
foreach($languages as $langId=>$lang) {
  $savedLang = explode(',',$user['lang']);

  $isSelected = in_array($langId, $savedLang)?'selected':'';

?>
       <option <?=$isSelected?> value="<?=$langId?>"><?=$lang?></option>
<?php
}
?>
      </select>
    </div>
  </div>


  <div class="form-group">
    <label class="col-sm-3 control-label">Hobbies</label>
    <div class="col-sm-5">

<?php
foreach($hobbies as $hobby_key=>$hobby_val)
{
  $savedhobbies = explode(',',$user['hobbies']);

  $isChecked = in_array($hobby_key, $savedhobbies)?'checked':'';

?>
      <div class="checkbox"><label>
        <input type="checkbox" <?=$isChecked?> name="hobbies[]" value="<?=$hobby_key?>"><?=$hobby_val?>
      </label></div>
<?php
}
?>
      <label for="hobbies[]" class="error"></label>

    </div>


  </div>

  <div class="form-group">
    <label  class="col-sm-3 control-label">Upload Avatar</label>
    <div class="col-sm-5">

      <img src="./uploads/wall/<?=$user['avatar']?>">
      <br>
      <input type="file" class="form-control" name="u_avatar" />

    </div>
  </div>

  <div class="form-group">
    <div class="col-sm-offset-3 col-sm-5">
      <button type="submit" class="btn btn-primary">Update</button>
    </div>
  </div>


</form>

<!----------------  FORM ------------>
      </div>
<!-------------- FOOTER START --------------------------->
<?php
  require_once('./includes/footer.php');
?>
